#ifndef CHATSERVER_H
#define CHATSERVER_H

#include <QWidget>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothaddress.h>
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QBluetoothServer>
#include <QBluetoothSocket>
namespace Ui {
class ChatServer;
}

class ChatServer : public QWidget
{
    Q_OBJECT

public:
    explicit ChatServer(QWidget *parent = nullptr);
    ~ChatServer();
    /* 开启服务端 */
    void startServer(const QBluetoothAddress &localAdapter =QBluetoothAddress());
    /* 停止服务端 */
    void stopServer();
public slots:
    /* 发送消息 */
    void sendMessage(const QString &message);
    /* 服务端主动断开连接 */
    void disconnect();
signals:
    /* 接收到消息信号 */
    void messageReceived(const QString &sender, const QString &message);
    /* 客户端连接信号 */
    void clientConnected(const QString &name);
    /* 客户端断开连接信号 */
    void clientDisconnected(const QString &name);
private slots:
    /* 客户端连接 */
    void clientConnected();
    /* 客户端断开连接 */
    void clientDisconnected();
    /* 读 socket */
    void readSocket();

private:
    /* 使用 rfcomm 协议 */
    QBluetoothServer *rfcommServer;
    /* 服务器蓝牙信息 */
    QBluetoothServiceInfo serviceInfo;
    /* 用于保存客户端 socket */
    QList<QBluetoothSocket *> clientSockets;
    /* 用于保存客户端的名字 */
    QList<QString> socketsPeername;
};

#endif // CHATSERVER_H
